import API from "@/api";
import type { ILessonResult } from "@/pages/lowPricedCourse/trialCourseHome/courseList/index.vue";

export interface ISkillDataResult {
  scoreLimit: number;
  session_id: string;
  sum_up_video: string;
  sum_up_video_process: number; // 汇总视频进度 0：还未观看 1:观看完 2:点击了下一页
  sum_up_video_id: number;
  skillData: ISkillData[]
}

export interface ISkillResult {
  score: number;
  scoreLimit: number;
  time_long: number;
  study_num: number;
  days: number;
  skillData: ISkillResult_skillData[];
}

export interface ISkillResult_skillData {
  part_id: string;
  symbol: string;
  practiceList: IPracticeList[],
}

export interface ISkillData {
  part_id: number;
  video: string;
  video_process: number; // 0：还未观看 1:观看完 2:点击了下一页
  complete: number; // 这一节是否完成 0:未完成 1:已完成
  practiceList: IPracticeList[];
}

export interface IPracticeList {
  practice_id: number;
  record_id: number;
  type: number; // 类型 0:单词 1:短语 2:句子
  practice: string;
  tag_practice: string;
  translate: null | string;
  uk_voice: string;
  us_voice: string;
  user_voice: string;
  analysis: any;
  score: number;
}

export const requestSoundExplanationData = (skillId: number, process: number): Promise<ILessonResult> => {
  return new Promise((resolve, reject) => {
    const axios = process >= 100 ? API.skillResult : API.skillData;

    axios({ id: skillId }).then((res: any) => {
      let result: any;

      if (process < 100) {
        result = res as ISkillDataResult;

        let isRelearn = false;
        let isLearnFinish = false;
        let questionArray: any[] = [];
        let currentQuestionIndex = 0;

        result.skillData.forEach((item: ISkillData) => {
          if (item.video) {
            questionArray.push({
              type: 3, //  0:单词 1:短语 2:句子 3：视频
              video: item.video,
              video_process: item.video_process,
              part_id: item.part_id,
            })
          }

          if (Array.isArray(item.practiceList) && item.practiceList.length) {
            questionArray.push(...item.practiceList);
          }
        })

        if (result.sum_up_video) {
          questionArray.push({
            type: 3,
            video: result.sum_up_video,
            video_process: result.sum_up_video_process,
            part_id: 0,
          })
        }

        for (let i = 0, len = questionArray.length; i < len; i++) {
          const el = questionArray[i];

          if (el.type === 3) {
            if (el.video_process != 2) {
              currentQuestionIndex = i;
              break;
            }
          } else if (!el.user_voice) {
            currentQuestionIndex = i;
            break;
          }
        }

        const lastItem = questionArray[questionArray.length - 1];

        if (lastItem.user_voice || lastItem.video_process == 2) {
          isRelearn = true;
          isLearnFinish = true;
        }

        let data: ILessonResult = {
          isRelearn: process >= 100,
          isNeedLaunchPage: false,
          data: {
            result,
            questionArray,
            currentQuestionIndex,
            nodeTotalNum: questionArray.length,
            isRelearn,
            isLearnFinish,
            skillId,
          },
        }

        resolve(data);
      } else {
        result = res as ISkillResult;

        let data: ILessonResult = {
          isRelearn: process >= 100,
          isNeedLaunchPage: false,
          data: {
            result,
            isRelearn: true,
            isLearnFinish: true,
            skillId,
          },
        }

        resolve(data);
      }
    }).catch(() => {
      reject();
    })
  })
}